﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Mengsk.ProxyDelivery.App.Extenstions;

namespace Mengsk.ProxyDelivery.App.Device.Kunhong
{
    /// <summary>
    /// KunhongConfigUI.xaml 的交互逻辑
    /// </summary>
    public partial class KunhongConfigUI : UserControl, IDeviceConfigUI
    {
        private KunhongDeviceConfigInfo configInfo = null;
        string path = App.DIR_CONFIG + "Device\\KunhongDevice.xml";

        public KunhongConfigUI()
        {
            InitializeComponent();
        }

        public Control GetControl()
        {
            return this;
        }

        public void Save()
        {
            this.btnSave_Click(null, null);
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            string item = this.cbbPorts.SelectedItem as string;

            if (string.IsNullOrWhiteSpace(item))
            {
                MessageBox.Show("请选择串口");
                return;
            }

            this.configInfo.SerialPort = item;
            try
            {
                (new FileInfo(this.path)).EnsureExits();
                using (Stream s = new FileStream(path, FileMode.Create))
                {
                    System.Xml.Serialization.XmlSerializer se = new System.Xml.Serialization.XmlSerializer(typeof(KunhongDeviceConfigInfo));
                    se.Serialize(s, this.configInfo);
                }
                MessageBox.Show("保存成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "保存配置出错");
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.cbbPorts.ItemsSource = System.IO.Ports.SerialPort.GetPortNames();

            if (File.Exists(path))
            {
                System.Xml.Serialization.XmlSerializer se = new System.Xml.Serialization.XmlSerializer(typeof(KunhongDeviceConfigInfo));
                try
                {
                    using (Stream s = new FileStream(path, FileMode.Open))
                    {
                        this.configInfo = (KunhongDeviceConfigInfo)se.Deserialize(s);
                        if (string.IsNullOrWhiteSpace(this.configInfo.SerialPort) == false)
                        {
                            this.cbbPorts.SelectedItem = this.configInfo.SerialPort;
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "加载配置文件出错");
                    File.Delete(path);
                }
                return;
            }
            this.configInfo = new KunhongDeviceConfigInfo();
            if (this.cbbPorts.Items.Count > 0)
            {
                this.cbbPorts.SelectedIndex = 0;
            }
        }
    }
}
